﻿using System;
using System.Collections;
using System.Configuration;
using System.Data; 
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Common.Entity;
using System.Collections.Generic;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Telerik.WebControls; 

namespace Runda.CMS.Portal.SysManage
{
    public partial class DepartmentList : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GenerateTreeView();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            this.RadTreeView1.NodeEdit += new RadTreeView.RadTreeViewEventHandler(RadTreeView1_NodeEdit);
        }

        private void GenerateTreeView()
        {
            this.RadTreeView1.Nodes.Clear();

            List<SqlDbParameter> parms = new List<SqlDbParameter>();
            SqlDbParameter parm = new SqlDbParameter();
            parm.ColumnName = "Deleted";
            parm.ColumnType = DbType.Boolean;
            parm.ParameterName = "@Deleted";
            parm.ParameterValue = false;
            parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
            parms.Add(parm);

            List<SysDepartmentEntity> DeptList = new SysDepartmentBll().GetAll<SysDepartmentEntity>(parms, "Order By DepartmentName Asc");

            List<SysDepartmentEntity> SubDeptList = DeptList.FindAll(delegate(SysDepartmentEntity e) { return e.ParentID == Guid.Empty; });
            foreach (SysDepartmentEntity ent in SubDeptList)
            { 
                RadTreeNode node = CreateNode(ent.DepartmentName,ent.DepartmentName,true,ent.DepartmentID.ToString(),true);
                RadTreeView1.Nodes.Add(node);

                RecursivelyPopulate(DeptList, node, true);
            }
            
        }

        private void RecursivelyPopulate(List<SysDepartmentEntity> DeptList, RadTreeNode node,bool CanEdit)
        {
            List<SysDepartmentEntity> SubDeptList = DeptList.FindAll(delegate(SysDepartmentEntity e) { return e.ParentID == new Guid(node.Value); });

            foreach (SysDepartmentEntity ent in SubDeptList)
            {
                RadTreeNode radTreeNode = CreateNode(ent.DepartmentName, ent.DepartmentName, true, ent.DepartmentID.ToString(), CanEdit);

                node.Nodes.Add(radTreeNode);
                RecursivelyPopulate(DeptList, radTreeNode, true);
            }
        }

        private RadTreeNode CreateNode(string text, string toolTip, bool expanded, string id, bool CanEdit)
        {
            RadTreeNode node = new RadTreeNode(text, id);
            node.Expanded = expanded;

            if (CanEdit)
            {
                node.ContextMenuName = "Management";
            }

            //if (id.ToLower() == Enumerator.PlanningFolderSystemId.SharedFolderId.ToLower())
            //{
            //    node.ImageUrl = "../Images/ico/ico_16_OrgChart.gif";
            //}
            //else if (id.ToLower() == Enumerator.PlanningFolderSystemId.DraftFolderId.ToLower())
            //{
            //    node.ImageUrl = "../Images/ico/ico_16_OrgChart.gif";
            //}
            
            return node;
        }

        protected void RadTreeView1_NodeContextClick(object o, Telerik.WebControls.RadTreeNodeEventArgs e)
        {
            if (e.ContextMenuItemID == "Delete")
            {
                SysDepartmentBll bll = new SysDepartmentBll();
                //检查下级是否有子节点
                List<SqlDbParameter> parms = new List<SqlDbParameter>();
                SqlDbParameter parm = new SqlDbParameter();
                parm.ColumnName = "Deleted";
                parm.ColumnType = DbType.Boolean;
                parm.ParameterName = "@Deleted";
                parm.ParameterValue = false;
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                parm = new SqlDbParameter();
                parm.ColumnName = "ParentID";
                parm.ColumnType = DbType.Guid;
                parm.ParameterName = "@ParentID";
                parm.ParameterValue = new Guid(e.NodeClicked.Value);
                parm.QualificationType = SqlDbParameter.QualificationSymbol.Equal;
                parms.Add(parm);

                List<SysDepartmentEntity> list = bll.GetAll<SysDepartmentEntity>(parms);

                if (list.Count > 0)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('该部门有下级部门，不能删除!');", true);
                    return;
                }

                bll.LogicDelete(new SysDepartmentEntity(new Guid(e.NodeClicked.Value)));

                GenerateTreeView();
            }
        }
        void RadTreeView1_NodeEdit(object o, RadTreeNodeEventArgs e)
        {

            if (e.NewText != "")
            {
                RadTreeNode nodeEdited = e.NodeEdited;

                SysDepartmentBll Bll = new SysDepartmentBll();
                SysDepartmentEntity entity = Bll.Get<SysDepartmentEntity>(new Guid(nodeEdited.Value));
                if (entity != null)
                {                    
                    entity.DepartmentName = e.NewText;
                    
                    entity.ModifiedBy = this.CurrentUserID;
                    entity.ModifiedOn = DateTime.Now;
                    Bll.Update(entity);
                }

                GenerateTreeView();
            }

        }

        protected void btnRefersh_Click(object sender, EventArgs e)
        {
            GenerateTreeView();
        }


    }
}
